package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.FileLibraryModel;
import com.ruoyi.system.domain.dto.KnowledgeBaseDto;
import com.ruoyi.system.mapper.FileLibraryMapper;
import com.ruoyi.system.service.KnowledgeBaseService;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther Zhipeng Lyu
 * @Date 2023/12/15
 * @Description 知识库管理服务实例类
 */

@Service
public class KnowledgeBaseServiceImpl extends ServiceImpl<FileLibraryMapper, FileLibraryModel> implements KnowledgeBaseService {
    // chatchat api基础路径
    private final String BASE_URL = "http://10.1.79.85:7861/knowledge_base";
    // 创建知识库url
    private final String CREATE_KB_API_URL = BASE_URL + "/create_knowledge_base";
    // 新增临时知识库url
    private final String CREATE_TEMP_KB_API_URL = BASE_URL + "/create_temp_knowledge_base";
    // 删除知识库url
    private final String DELETE_KB_API_URL = BASE_URL + "/delete_knowledge_base";
    public Boolean CreateKnowledgeBase(String kbName){

        try{
            RestTemplate restTemplate = new RestTemplate();

            // 创建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 创建请求体
            Map<String, String> body = new HashMap<>();
            body.put("knowledge_base_name", kbName);
            body.put("vector_store_type", "faiss");
            body.put("embed_model", "m3e-base");

            // 创建 HttpEntity 对象
            HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);

            // 发送 POST 请求并获取响应
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(CREATE_KB_API_URL, entity, String.class);

            // 解析响应体
            JSONObject response = new JSONObject(responseEntity.getBody());

            // 打印响应
//            System.out.println("Code: " + response.getInt("code"));
//            System.out.println("Message: " + response.getString("msg"));
            if(response.getInt("code") == 200){
                return true;
            }else{
                log.error(response.getString("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public Boolean CreateTempKnowledgeBase(KnowledgeBaseDto knowledgeBaseDto){
        try{
            // 获取源知识库名、临时知识库名以及选择的文件名列表
            String kbName = knowledgeBaseDto.getKnowledgeBaseName();
            String tempKbName = knowledgeBaseDto.getTempKnowledgeBaseName();
            List<String> fileNames = knowledgeBaseDto.getFileNames();
            RestTemplate restTemplate = new RestTemplate();

            // 创建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 创建请求体
            Map<String, Object> body = new HashMap<>();
            body.put("knowledge_base_name", kbName);
            body.put("temp_knowledge_base_name", tempKbName);
            body.put("file_names", fileNames);

            // 创建 HttpEntity 对象
            HttpEntity<Map<String, Object>> entity = new HttpEntity<>(body, headers);

            // 发送 POST 请求并获取响应
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(CREATE_TEMP_KB_API_URL, entity, String.class);

            // 解析响应体
            JSONObject response = new JSONObject(responseEntity.getBody());

            // 打印响应
//            System.out.println("Code: " + response.getInt("code"));
//            System.out.println("Message: " + response.getString("msg"));
            if(response.getInt("code") == 200){
                return true;
            }else{
                log.error(response.getString("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public Boolean DeleteKnowledgeBase(String kbName){
        try{
            RestTemplate restTemplate = new RestTemplate();

            // 创建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            // 创建请求体
            String body = String.format("\"%s\"",kbName);

            // 创建 HttpEntity 对象
            HttpEntity<String> entity = new HttpEntity<>(body, headers);

            // 发送 POST 请求并获取响应
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(DELETE_KB_API_URL, entity, String.class);

            // 解析响应体
            JSONObject response = new JSONObject(responseEntity.getBody());

            // 打印响应
//            System.out.println("Code: " + response.getInt("code"));
//            System.out.println("Message: " + response.getString("msg"));
            if(response.getInt("code") == 200){
                return true;
            }else{
                log.error(response.getString("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

}
